var setting = {
    check: {
        enable: false,
        autoCheckTrigger: true,
        chkStyle : "checkbox"
    },
    view: {
        showIcon: false
    },
    data: {
        simpleData: {
            enable: true
        }
    },
    callback: {
        onClick: zTreeOnClick
    }
};
/*点击节点的事件*/
function zTreeOnClick(event, treeId, treeNode) {
    // alert(treeNode.name);
    $("#roleId").val(treeNode.id);
    query(1);
}
//树形的数据
var zNodes ;
function setCheck() {
    var zTree = $.fn.zTree.getZTreeObj("treeDemo");
    zTree.setting.check.chkboxType = {"Y": 'ps', "N": 'ps'};
}
function query(pageNum) {
    var roleId = $("#roleId").val();
    var name = $("#name").val();
    var eid = $("#eid").val();
    var cid = $("#cid").val();
    $.post(
        "/admin/roleQuery",
        {
            roleId:roleId,
            name:name,
            eid:eid,
            cid:cid,
            // pageSize:2,
            pageNum:pageNum
        },
        function(data,status){
            // console.log('data:'+JSON.stringify(data));

            var $body = $("table.role-table > tbody");
            // alert($body.html());
            var list = data.list;
            var content = '';
            for(var i=0;i<list.length;i++){
                var node = list[i];
                content = content+'<tr>';
                content = content+'<td>'+(i+1)+'</td>';
                content = content+'<td>'+node.id+'</td>';
                content = content+'<td>'+node.name+'</td>';
                content = content+'<td>'+node.roleName+'</td>';
                content = content+'<td>'+node.idCard+'</td>';
                content = content+'<td><a href="/admin/modifyRole?eid='+node.id+'"><span class="modify"><i class="fa fa-gear"></i>修改角色</span></a></td>';
                content = content+'</tr>';
            }
            $body.html(content);
            //分页

            var totalCount = data.totalCount;
            var totalPage = data.totalPage;
            console.log('totalCount:'+totalCount);
            var pageNum = data.pageNum;
            var start = data.start;
            var end = data.end;
            $("#totalPage").val(totalPage);
            $("#pageNum").val(pageNum);
            console.log('pageNum:'+pageNum);
            if(totalPage === 0){
                $(".no-data").css("display","block");
                $(".page-list").hide();
            }else {
                $(".no-data").hide();
                // $(".page-list").show();
                var htmlContent = '';
                htmlContent = htmlContent+'<span id="totalCount">共'+totalCount+'条</span>';
                if((pageNum-start)>0){
                    htmlContent = htmlContent+'<a href="javascript:queryByPageNum('+(pageNum-1)+');">上一页</a>';
                }
                for(var index = start;index<=end;index++){
                    htmlContent = htmlContent+'<a '+((pageNum===index)?'class="active "':'')+' href="javascript:queryByPageNum('+index+');">'+index+'</a>';
                }
                if((end-pageNum)>0){
                    htmlContent = htmlContent+'<a href="javascript:queryByPageNum('+(pageNum+1)+');">下一页</a>';
                }
                htmlContent = htmlContent+'<span>跳转到<input id="jumpPage" type="number" value="">页</span>';
                htmlContent = htmlContent+'<span class="jump" onclick="queryByJump()" >跳转</span>';
                $(".page-list").show().html(htmlContent);
            }



            // alert("数据: \n" + data + "\n状态: " + status);
        });
}
$(document).ready(function(){

    if(typeof(zNodes)==='undefined'){
        $.get("/admin/initRoleList",function(data,status) {
            // console.info('data:'+JSON.stringify(data));
            // console.info('status:'+JSON.stringify(status));
            if (status) {
                zNodes = data;
                $.fn.zTree.init($("#treeDemo"), setting, zNodes);
                setCheck();
                query(1);
            }
        });
    }


});

function queryByPageNum(currentPageNum) {
    query(currentPageNum);
}
function queryByJump(){
    var jumpPage = $("#jumpPage").val();
    // alert('test:');
    var totalPage = $("#totalPage").val();
    var pageNum = $("#pageNum").val();
    if(typeof(jumpPage) !== 'number' && (jumpPage>totalPage || jumpPage<1)){
        alert("请输入合法页码!");
        return;
    }
    query(jumpPage);
}

function queryByCondition() {
    query(1);
}